#include "../comm.h"
#include <numeric>
class Solution {
public:
    // 让每个时间段的任务尽可能的多
    // 尽量推迟完成时间，一次性完成的任务更多
    // 按照结束时间进行升序排序，能保证完成前面的任务之后后面的任务一定不会过期
    int n;
    int findMinimumTime(vector<vector<int>>& tasks) {
        n = tasks.size();
        vector<int> top;
        for(int i = 0; i < n; ++i) top.push_back(i);
        sort(top.begin(), top.end(), [&](const int a, const int b){
            return tasks[a][1] < tasks[b][1];
        });
        // int sum[2020] = { 0 };
        bool flag[2020] = { false };
        int ans = 0;
        for(int i = 0; i < n; ++i)
        {
            int j = top[i];
            int st = tasks[j][0], ed = tasks[j][1], num = tasks[j][2];
            int now = 0;
            for(int i = st; i <= ed; ++i)
                if(flag[i])
                {
                    now++;
                    if(now == num) break;
                }
            if(now < num)
                for(int i = ed; i >= st; --i)
                {
                    if(!flag[i]) flag[i] = true, now++, ans++;
                    if(now == num) break;
                }
        }
        return ans;
    }
};